package com.dongdongshop.mq;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.service.TbUserScoreAccountservice;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "txmq-group2",topic = "tx-integral")
public class MQConsumerLinster implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Reference
    private TbUserScoreAccountservice tbUserScoreAccountservice;
    @Override
    public void onMessage(String s) {
        System.out.println("消息消费成功{}{}{}" + s);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    MessageExt messageExt = list.get(0);
                    byte[] body = messageExt.getBody();
                    String s = new String(body);
                    TbUserScoreAccount tsa = (TbUserScoreAccount) JSONObject.parseObject(s,TbUserScoreAccount.class);
                    //获取用户名
                    String userid = tsa.getUserid();

                    //判断此次消费是否为同一个用户
                    TbUserScoreAccount acc = tbUserScoreAccountservice.selectByUserId(userid);
                    if (acc != null) {//此用户消费过

                        //此用户已经消费过  再选有的积分上进行追加
                        String integral = acc.getIntegral();
                        String integral1 = tsa.getIntegral();
                        BigDecimal b = new BigDecimal(integral);
                        BigDecimal b2 = new BigDecimal(integral1);
                        BigDecimal add = b.add(b2);
                        acc.setIntegral(add.toString());

                        tbUserScoreAccountservice.updateAcc(acc);
                    }else {
                        tsa.setCratetime(new Date());
                        tsa.setUpdatetime(new Date());
                        tbUserScoreAccountservice.insertTsa(tsa);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}